<template>
	<view class="setting-page">
		<view class="line-area">
			<view class="setting-item" v-for="v in lineMsg" @click="togo(v.url)">
				<view class="setting-left">{{v.left}}</view>
				<view class="setting-right">{{v.right}} > </view>
			</view>
		</view>
		<view class="line-area">
			<view class="setting-item" v-for="v in otherMsg"  @click="togo(v.url)">
				<view class="setting-left">{{v.left}}</view>
				<view class="setting-right other-right">{{v.right}} > </view>
			</view>
		</view>
		<view  class="exit" @click="onexit">
			退出登录
		</view>
	</view>
</template>

<script setup>
	import { showConfirmDialog } from 'vant';
	import api from '../../lib/request';
	import { showToast } from 'vant';
	import {user} from '../../pinia/user.js'
	const User = user()
	import {ref} from 'vue'
	let token = uni.getStorageSync('token')
	let lineMsg = ref([
		{
			left: '账号信息',
			right: '去查看',
			url: '/views/account/account'			
		},
		{
			left: '登录密码',
			right: '去修改',
			url: '/views/changepass/changepass'
		},
		{
			left: '注销用户',
			right: '',
			url: '/views/logoff/logoff'
		}
	])
	
	let otherMsg = [
		{
			left: '隐私权政策',
			right: '',
			url: ''
		},
		{
			left: '用户协议',
			right: '',
			url: ''
		},
		{
			left: '关于我们',
			right: '',
			url: '/views/aboutme/aboutme'
		},
	]
	
	function togo(url){
		if(!token){return showToast('请先登录')}
		console.log(url)
		// 点击了注销
		if(url =='/views/logoff/logoff'){
			showConfirmDialog({
			  title: '注销用户',
			  message: '注销成功后,微医平台账号登录的服务将无法再使用此账号' })
			  .then(() => {
				api.del('/user/user/del').then(res=>{
					if(res.code){
						showToast({duration:1500,message:'谢谢使用,再见'})
						uni.removeStorageSync('token')
						User.updateUserMsg({})
						User.updateUserStatus(0)
						setTimeout(()=>{
							uni.reLaunch({url:'/pages/home-page/home-page'})
						},1500)
					}
				})
			  })
			  .catch(() => { });
			  return
		}
		uni.navigateTo({url})
	}
	
	
	// 退出登录
	function onexit(){
		// if(!token){return showToast('请先登录')}
		showConfirmDialog({
		  title: '退出登录',
		  message:
		    '您确定要退出吗?',
		})
		  .then(() => {
		    // on confirm
			console.log('点击了确认按钮')
			api.post('/user/user/logout').then(res=>{
				if(res.code){
						showToast({duration:1500,message:'退出成功'});
						uni.clearStorageSync()
						User.updateUserMsg({})
						User.updateUserStatus(0)
					setTimeout(()=>{
						uni.reLaunch({url:'/pages/home-page/home-page'})
					},1500)
				}
				
			})
		  })
		  .catch(() => { });
	}
</script>

<style lang="scss" scoped>
	.setting-page {
		width: 100%;
		height: calc(100vh - 44px);
		background-color: #f7fffd;
	}

	.line-area {
		width: 100%;
		padding: 10px 0;
		// height: ;
	}

	.setting-item {
		width: 100%;
		box-sizing: border-box;
		background: #fff;
		border-bottom: 1px solid #eee;
		padding: 15px 10px;
		width: 100%;
		display: flex;
		justify-content: space-between;
	}

	.setting-right {
		color: royalblue
	}
	
	.other-right{
		color:#000;
	}
	.exit{
		width:100%;
		height: 50px;
		margin-top: 10px;
		background-color: #fff;
		line-height: 50px;
		text-align: center;
		font-size: 18px;
	}
</style>